package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：zhao
 * @date ：Created in 2020/1/2 0002
 * @description ：角色控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 查询所有角色
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Role> roleList = roleService.findAll();
        return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, roleList);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS, pageResult);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Role role = roleService.findById(id);
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS, role);
    }

    /**
     * 根据id查询权限id集合
     *
     * @param id
     * @return
     */
    @GetMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, permissionIds);
    }

    /**
     * 根据id查询菜单id集合
     *
     * @param id
     * @return
     */
    @GetMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, menuIds);
    }

    /**
     * 添加
     *
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleService.add(role, permissionIds, menuIds);
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        roleService.delete(id);
        return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleService.edit(role, permissionIds, menuIds);
        return new Result(true, MessageConst.EDIT_ROLE_SUCCESS);
    }
}
